<!-- 数据字典选择下拉 -->
<template>
	<view class="">
		<view style="width: 100%;" @click="onShowPicker">
			<u--input
				:placeholder="placeholder"
				:placeholderClass="placeholderClass"
				:placeholderStyle="placeholderStyle"
				:inputAlign="inputAlign"
				:fontSize="fontSize"
				:color="color"
				:prefixIcon="prefixIcon"
				:prefixIconStyle="prefixIconStyle"
				:suffixIcon="suffixIcon"
				:suffixIconStyle="suffixIconStyle"
				:border="border"
				:shape="shape"
				:customStyle="customStyle"
				v-model="title"
				disabled
				:disabledColor="disabledColor"
				
			></u--input>
		</view>
		<u-picker 
			:show="showPicker" 
			:defaultIndex="defaultIndex" 
			:loading="loading" 
			:columns="listByCodes" 
			keyName="title" 
			@confirm="onConfirm" 
			@cancel="onCancel"
		></u-picker>
	</view>
</template>

<script>
	import { 
		queryDictModelByCode, // 
		ajaxGetDictItems, //字典标签专用（通过code获取字典数组）
		serviceAjaxGetDictItems, // 获取系统级字典
	} from '@/api/index.js'
	import { initDictOptions } from '@/utils/JDictSelectUtil.js'
	
	export default {
		name: 'hDictSelect',
		data() {
			return {
				title:'', // 选中的标题名称
				showPicker: false, // 是否显示弹窗
				listByCodes: [], // 字典列表
				loading: false, // 是否加載
				defaultIndex: [0], // 弹窗默认选中的列 - 默认选中第一列
			}
		},
		model: {
			prop: 'value',
			event: 'change',
		},
		props: {
			// 字典编号
			dictName: {
				type: String,
				default: ''
			},
			// 是否禁用
			disabled: {
				type: Boolean,
				default: false
			},
			// 选中的值
			value: {
				type: [String, Number],
				default: '',
			},
			// 输入框为空时的占位符
			placeholder: {
				type: [String, Number],
				default: '请选择',
			},
			// 指定placeholder的样式类，注意页面或组件的style中写了scoped时，需要在类名前写/deep/
			placeholderClass: {
				type: [String],
				default: '',
			},
			// 指定placeholder的样式，字符串/对象形式，如"color: red;"
			placeholderStyle: {
				type: [String, Object],
				default: 'color: #C0C4CC'
			},
			// 输入框内容对齐方式 - left | center | right
			inputAlign: {
				type: [String,],
				default: 'left',
			},
			// 输入框字体的大小
			fontSize: {
				type: [String, Number],
				default: '15px',
			},
			// 输入框字体颜色
			color: {
				type: [String],
				default: 'inherit',
			},
			// 输入框前置图标
			prefixIcon: {
				type: [String],
				default: '',
			},
			// 前置图标样式，对象或字符串
			prefixIconStyle: {
				type: [String, Object],
				default: '',
			},
			// 输入框前置图标
			suffixIcon: {
				type: [String],
				default: 'arrow-down-fill',
			},
			// 前置图标样式，对象或字符串
			suffixIconStyle: {
				type: [String, Object],
				default: 'color: #B2B2B2',
			},
			// 边框类型，surround-四周边框，bottom-底部边框，none-无边框
			border: {
				type: [String],
				default: 'surround',
			},
			// 输入框形状，circle-圆形，square-方形
			shape: {
				type: [String],
				default: 'square',
			},
			customStyle: {
				type: [Object],
				default: () => {},
			}
			
		},
		watch: {
			dictName: {
				handler (val) {
					if (val) {
						this.queryDictModelByCodes(val)
					}
				},
				immediate: true
			},
			// 监听哪个有值就先走哪个保证能回显title
			value: {
				handler (val) {
					if (!val) {
						this.title = ''
						this.defaultIndex = [0] // 弹窗默认选中的列 - 默认选中第一列
					} else {
						if (this.listByCodes && this.listByCodes.length) {
							this.listByCodes[0].map((value, index) => {
								if (value.value == val) {
									this.title = value.title
									this.defaultIndex = [index]
								}
							})
						}
					}
				},
				immediate: true
			},
			listByCodes: {
				handler (val) {
					if (val && val.length) {
						val[0].map((item, index) => {
							if (item.value == this.value) {
								this.title = item.title
								this.defaultIndex = [index]
							}
						})
					}
				},
				immediate: true
			}
		},
		computed: {
			// 当是disabled为true改变禁用的颜色
			disabledColor () {
				if (this.disabled) {
					return '#f5f7fa'
				} else {
					return '#FFFFFF'
				}
			}
		},
		methods: {
			// 点击弹出弹框
			onShowPicker() {
				if (this.disabled) {
					return false
				}
				if (!this.listByCodes || !this.listByCodes.length) {
					uni.showToast({
						title: '暂无数据',
						icon: 'none'
					})
					return false
				}
			  this.showPicker = true
			},
			
			//字典
			queryDictModelByCodes(name) {
				this.loading = true
				initDictOptions(name).then(res => {
					this.listByCodes = []
					if (res.success) {
						if (res.result && res.result.length) {
							this.listByCodes.push(res.result)
						} else {
							this.listByCodes = []
						}
					}
					this.loading = false
				}).catch(err => {
					this.loading = false
				})
			},
			// 点击确定
			onConfirm (item) {
				this.title = item.value[0].title
				this.$emit('change',item.value[0].value);
				this.$emit('input',item.value[0].value);
				this.$emit('back',item.value[0]);
				this.showPicker = false
			},
			
			// 点击取消
			onCancel () {
				this.showPicker = false
			},
			
			// 选择生产状态
			// codesClick (check,index) {
			// 	if(!check) {
			// 		this.listByCodes[this.oldzgqkindex].check = false
			// 		this.listByCodes[index].check = true
			// 		// this.form.sczt = this.listByCodes[index].value
			// 		this.$emit('codesClick', this.listByCodes[index].value)
			// 	} else {
			// 		this.listByCodes[index].check = false
			// 		this.$emit('codesClick', '')
			// 	}
			// 	this.oldzgqkindex = index
			// },
		},
		mounted() {
			//初始化数据
			// this.queryDictModelByCodes(this.dictName);
		},
	}
</script>

<style lang="scss" scoped>
	
</style>